package demo2.datasource;

import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.csp.sentinel.datasource.FileRefreshableDataSource;
import com.alibaba.csp.sentinel.init.InitFunc;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.fastjson.JSON;

import java.net.URL;

public class MyDatasource implements InitFunc {
    @Override
    public void init() throws Exception {
        System.out.println("数据源被加载....");
        URL datasource = this.getClass().getClassLoader().getResource("flowRules.json");
        String path = datasource.getPath();
        System.out.println("拿到路径:"+path);
        FileRefreshableDataSource fileRefreshableDataSource = new FileRefreshableDataSource(path, new Converter() {
            @Override
            public Object convert(Object o) {
                String json = (String) o;
                return JSON.parseArray(json);
            }
        });
        FlowRuleManager.register2Property(fileRefreshableDataSource.getProperty());
    }
}
